/**
This file is part of Gel2DE.

    Gel2DE is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Gel2DE is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with Gel2DE.  If not, see <http://www.gnu.org/licenses/>.

	Copyright (C) 2012, University of Bergen
*/

#include "GelImage.h"
#include "vtkTIFFWriter.h"
#include "vtkImageShiftScale.h"
#include "vtkInteractorStyleImage.h"
#include "ProjectConfiguration.h"
#include "GelImageTransformation.h"
#include <map>
#include "File.h"
#include "itkFlipImageFilter.h"
#include "itkFixedArray.h"
#include "vtkFileOutputWindow.h"



#ifndef GELCORRELATIONANALYSIS_H
#define GELCORRELATIONANALYSIS_H

class vtkColorTransferFunction;

/************************************************************************/
/* Class for doing the actual correlation analysis
/* Holds a stack of gel images with corresponding transformations
/* and T values.
/************************************************************************/

class GelCorrelationAnalysis
{
public:

	typedef BYTE RGBComponentType;
	typedef itk::RGBPixel<RGBComponentType> RGBPixelType;
	typedef itk::Image<RGBPixelType, 2> RGBImageType;
	typedef itk::ImageFileWriter<RGBImageType> RGBWriterType;
	typedef itk::ImageFileReader<RGBImageType> RGBReaderType;
	typedef itk::ImageRegionIterator<RGBImageType> RGBImageIteratorType;
	typedef itk::FlipImageFilter<RGBImageType> RGBFlipImageFilter;

	typedef double DataPixelType;
	typedef itk::Image<DataPixelType, 2> DataImageType;
	typedef itk::ImageRegionIterator<DataImageType> DataImageIteratorType;

	GelCorrelationAnalysis(vtkFileOutputWindow *aLogFile);
	~GelCorrelationAnalysis();

	void LoadFiles(std::string filename);
	void RunAnalysis(std::set<const std::string> aListOfParameters);
	void RunPermutationTest(DataImageType::RegionType aRoi, std::set<const std::string> aListOfParameters, bool aWriteToFile);

	void NormalizeImage( DataImageType::Pointer outImgD, DataImageType::RegionType outputRegion, DataPixelType aMaxValue);
	void WriteResultFile(std::string aImageName, std::string aIdString, gel2de::File* aPath, RGBImageType::Pointer aOutImg);
	void Clear();
	std::vector<std::string>* GetFiles();
	std::vector<GelImage*>* GetGelImages();
	GelImage* GetImage(int);
	void ToXML(const char*);
	void FromXML(const char*);
	void ExportData(GelCorrelationAnalysis::DataImageType::RegionType aRoi, std::string aFileName);
	void LoadConfiguration(ProjectConfiguration*);
	std::set<const std::string> getAllParameterTypes();
	void DataRoiToGelRoi(DataImageType::RegionType *aDataRoi, GelImage::ImageType::RegionType *aGelRoi);
	void SetBlurLevel(double aSigma);
	void SetNormalizationMode(int aNormalizationMode);

	gel2de::File* GetPreviousAnalysisOutputDir();
	std::string GetPreviousIdString();

	int NumImages() {return m_gelVector->size();}

	void SetColorTransferFunction(vtkColorTransferFunction *ctf);
	void Permute(std::vector<double>* aVector);
 
	DataImageType::Pointer getCImage();
	DataImageType::Pointer getSImage();
	DataImageType::Pointer getPImage();
	DataImageType::Pointer getDImage();
	DataImageType::Pointer getPTImage();
	DataImageType::Pointer getCSImage();
	DataImageType::Pointer getCSDImage();

	int GetNumberOfActiveGels(std::string aParameter);
	double GetAverage(std::string aParameter);
	GelCorrelationAnalysis::RGBImageType::Pointer FillRGBImage(
		GelImage::ImageType::RegionType region, 
		const GelImage::ImageType::SpacingType spacing,
		const GelImage::ImageType::PointType inputOrigin,
		DataImageType::RegionType outputRegion,
		DataImageIteratorType outIter);


private:
	void LoadGelImageTransformationMap(std::string aFileName);
	void LoadImageAndClinicalParameters(std::string aFileName, std::vector<std::string*>* aExcludedList);
	void SetOutputDirectory(gel2de::File* aFile);
	RGBPixelType* ToRGB(double);

	RGBImageType::Pointer createRGBImage(
		GelImage::ImageType::RegionType,
		const GelImage::ImageType::SpacingType&,
		const GelImage::ImageType::PointType&);

	DataImageType::Pointer createDataImage(
		GelImage::ImageType::RegionType,
		const GelImage::ImageType::SpacingType&,
		const GelImage::ImageType::PointType&);

	std::vector<GelImage*>* m_gelVector;
	std::map<const std::string, GelImageTransformation*>* m_gelImageTransformationMap;

	DataImageType::Pointer m_outImgC;
	DataImageType::Pointer m_outImgS;
	DataImageType::Pointer m_outImgD;
	DataImageType::Pointer m_outImgP;
	DataImageType::Pointer m_outImgPT;
	DataImageType::Pointer m_outImgCS;
	DataImageType::Pointer m_outImgCSD;

	vtkColorTransferFunction *m_ctf;

	gel2de::File* m_outputDirectory;
	gel2de::File* m_previousAnalysisOutputDir;
	std::string m_idstring;

	vtkFileOutputWindow *m_logFile;

  

};

#endif // GEL_CORRELATIONANALYSIS_H
